package com.camp.android.trip.n.track.menu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.camp.android.trip.n.track.DatabaseManager;
import com.camp.android.trip.n.track.Item;
import com.camp.android.trip.n.track.R;
import com.camp.android.trip.n.track.Trip;
import com.camp.android.trip.n.track.trip.TripItineraryActivity;

public class PastListAdapter extends BaseAdapter implements OnClickListener {

	private LayoutInflater layoutInflater;
	private ArrayList<Trip> itemList = null;
	private LayoutInflater inflater = null;
	private DatabaseManager dbmanager = null;
	private Activity activity = null;
	
	public PastListAdapter(LayoutInflater inflater, Activity activity) {
		 this.inflater = inflater;
		 this.activity = activity;
		 this.dbmanager = new DatabaseManager(activity);
		 
		 itemList = new ArrayList<Trip>();
		 itemList = dbmanager.getPastTrips();
		 
		 Log.i("TAG","PastListAdapter Initialized");
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		Log.i("Tag","Past.itemList.size = "+itemList.size());
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return itemList.get(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// Get custom item view
		View itemView = inflater.inflate(R.layout.menu_item, parent, false);
		// Get item object
		Trip trip = (Trip)getItem(position);

		float trip_rating = 0;
		
		ArrayList<Item> items = new DatabaseManager(activity).getItems(trip.id);
		if(items.size() > 0) {
			
			for(int i = 0; i < items.size(); i++) {
				trip_rating += items.get(i).rating;
			}
			trip_rating = trip_rating / items.size();
		}
		Log.i("RATING2",trip.items.size()+" --- "+trip_rating);
		// Fill custom view with object's properties
		((TextView) itemView.findViewById(R.id.item_name)).setText(trip.name);
		((RatingBar) itemView.findViewById(R.id.item_rating)).setRating(trip_rating);
		((TextView) itemView.findViewById(R.id.item_id)).setText(trip.id+"");
		// Initialize date format
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
		((TextView) itemView.findViewById(R.id.item_date)).setText(dateFormat.format(trip.start.getTime()));
		
		itemView.setOnClickListener(this);
		return itemView;
	}
	
	@Override
	public void onClick(View v) {
		Log.i("TAG","Item Clicked "+ ((TextView)v.findViewById(R.id.item_id)).getText());
		 
		// Create new intent to TripItineraryActivity
		Intent intent = new Intent(activity,TripItineraryActivity.class);
		// Add trip id to extra for itinerary's initialization
		intent.putExtra("trip_id", ((TextView)v.findViewById(R.id.item_id)).getText());
		// Start activity
		activity.startActivity(intent);
	}

}
